import * as Koa from 'koa'
import { Context } from 'koa'
import * as koaBody from 'koa-body'
import * as koaLogger from 'koa-logger'
import { api } from './api/api'
import config from './config'
import { logger } from './ins'
import { init } from './init'
import redisCluster from './util/redisClient'


var fs = require('fs')


init().then(() => {
  // 1. 验证 Redis 连接
  return redisCluster.ping().then(() => {
    logger.info('Redis cluster connection verified');
  });
}).then(() => {
  const app = new Koa()
  app.use(koaLogger())
  app.use(koaBody())
  const router = api()
  app.use(router.routes())

  app.listen(config.port)

  logger.info({ msg: "server start..." })

  // 未捕获的Api异常
  app.on('request-error', (error: Error, ctx: Context) => {
    logger.error({
      type: 'request-error',
      message: error.message,
      requestId: ctx.requestId
    })
  })

  // 未捕获的服务异常
  app.on('server-error', (error: any) => {
    logger.error({
      type: 'server-error',
      message: error.message
    })
  })
  // 未捕获的异常
  process.on('uncaughtException', err => {
    logger.error({ msg: `未捕获的异常:${err}` })
  })
}).catch((err) => {
  // 捕获初始化过程中的任何错误
  process.exit(1);
});

